<?php
	session_start();
	if(!$_SESSION['login'] == 1 && $_SESSION['sessionid'] !== session_id()) {
		header("Location: ./");
		exit;
	}
	
	if(isset($_GET['mode']) && $_GET['mode'] == "del" && !empty($_GET['id'])) {
		ob_start();
		include("include/functions.php");
		$id = $_GET['id'];
		$contacts->rm_contact($id);
		header("Location: home.php");
		ob_end_flush();
		exit;
	}

	include("include/class.mysql.php");
	include("include/layout.php");

	html_start();
?>

			<br />
			<span class="conin">
				<table class="contact_top">
					<tr>
						<td style="padding:10px 0 0 4px;">
							<form action="<?=$_SERVER['PHP_SELF'];?>" method="get" name="sub">
							<?=ADDR_FIND;?>
							<select name="x">
								<option value="f_name"><?=ADDR_FIRST;?></option>
								<option value="l_name"><?=ADDR_LAST;?></option>
								<option value="mail">Mail</option>
							</select> 
							<select name="y">
								<option value="contains"><?=ADDR_CONTAIN;?></option>
								<option value="is"><?=ADDR_IS;?></option>
							</select> 
							<input type="text" name="z" class="search" /> 
							<input type="submit" class="sub" value="<?=ADDR_SEARCH;?>" />
							</form>
						</td>
					</tr>
				</table>
				<br />
				<table class="contact_top">
					<tr>
						<td style="text-align:center;">
							<?php
								echo "| ";
								$alpha = array("A", "B", "C", "D", "E", "F", "G" , "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
								foreach($alpha as $letter) {
									echo "<a href=\"home.php?stl=" . $letter . "\">" . $letter . "</a> | ";
								}
							?>
						</td>
					</tr>
				</table>
				<br />
				<table class="contact">
					<tr style="border-bottom:1px dotted #5a4d4d;background-color:#F0F0F0;">
						<td style="padding:5px 5px;">&nbsp;</td>
						<td><b><?=ADDR_FULL;?></b></td>
						<td><b><?=ADDR_MAIL;?></b></td>
						<td><b><?=ADDR_PHONE;?></b></td>
						<?php
							if($_SESSION['priv'] >= 2) {
						?>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<?php
							}
						?>
					</tr>
					<?php
						$MySQL->conn();
						$group = $_SESSION['group'];
						$sql = "SELECT * FROM " . DB_PREFIX . "contacts WHERE user_group = '$group'";
						if(isset($_GET['stl'])) {
							$stl = $_GET['stl'];
							$sql .= "AND l_name LIKE '$stl%'";
						}
						$pos = $_GET['pos'];
						if(!isset($pos)) {
							$pos = 0;
						}
						
						else {
							$pos = $_GET['pos'];
						}
						
						if(isset($_GET['x']) && isset($_GET['y']) && isset($_GET['z'])) {
							$x = $_GET['x'];
							$y = $_GET['y'];
							$z = $_GET['z'];

							$sql .= "AND $x ";
							
							if($y == "contains") {
								$sql .= "LIKE '%$z%'";
							}
							
							elseif($y == "is") {
								$sql .= "= '$z'";
							}
							
							else {
								echo "2 An error occured!";
								exit;
							}
						}
						
						$sql .= "ORDER BY l_name ASC";
						$sql .= " LIMIT $pos,$limit";

						$sql = mysql_query($sql) or die(mysql_error());
						$num = mysql_num_rows($sql) or die(mysql_error());
						$sql2 = mysql_query("SELECT * FROM " . DB_PREFIX . "contacts WHERE user_group = '$group'") or die(mysql_error());
						$num2 = mysql_num_rows($sql2) or die(mysql_error());
						
						$tab_color = array("#F0F0F0", "#FDFDFD");
						$i = 1;
						
						while($row = mysql_fetch_array($sql)) {
					?>
					<tr style="border-bottom:1px dotted #5a4d4d;background-color:<?=$tab_color[$i % 2];?>">
						<td style="width:5%;padding:5px 5px;"><a href="view.php?id=<?=$row['id'];?>"><?=ADDR_VIEW;?></a></td>
						<td style="width:<?if($_SESSION['priv'] >= 2){?>32%<?} else {?>35%<?}?>;text-align:left;"><?=strip_tags($row['l_name']) . ", " . strip_tags($row['f_name']);?></td>
						<td style="width:<?if($_SESSION['priv'] >= 2){?>32%<?} else {?>37%<?}?>;text-align:left;">
						<?php
							if(!empty($row['mail'])) {
								echo "<a href=\"mailto:" . strip_tags($row['mail']) . "\">" . strip_tags($row['mail']) . "</a></td>";
							}
							
							else {
								echo "None";
							}
						?>
						</td>
						<td style="width:<?if($_SESSION['priv'] >= 2){?>17%<?} else {?>23%;<?}?>text-align:left;">
						<?php
							if(!empty($row['ph_home'])) {
								echo "<b>H</b> " . strip_tags($row['ph_home']);
							}
							elseif(!empty($row['ph_mobile'])) {
								echo "<b>M</b> " . strip_tags($row['ph_mobile']);
							}
							
							elseif(!empty($row['ph_work'])) {
								echo "<b>W</b> " . strip_tags($row['ph_work']);
							}
							
							else {
								echo "None";
							}
						?></td>
						<?php
							if($_SESSION['priv'] >= 2) {
						?>
						<td style="width:3%;text-align:left;"><a href="edit_contact.php?id=<?=$row['id'];?>"><img src="img/edit.png" style="border:0px;" alt="" /></a></td>
						<td style="width:3%;text-align:left;"><a href="javascript:if(confirm('<?=ADDR_SURE;?>')){location.href='home.php?mode=del&id=<?=$row['id'];?>';}"><img src="img/del.png" style="border:0px;" alt="" /></a></td>
						<?php
							}
						?>
					</tr>
					<?php
						$i++;
						}
						
						$MySQL->close();
					?>
				</table>
				<br />
				<table class="contact_top">
					<tr>
						<td style="width:15%;text-align:left;">
						<?php
							$next = $pos+$limit;
							$prev = $pos-$limit;
							if(($num-$pos) < $num) {
								echo "&lt; <a href=\"home.php?pos=" . $prev . "\">" . ADDR_PREVIOUS . "</a>";
							}
						?></td>
						<td style="width:70%;text-align:center;">
						<?php
							$nr = $num2/$limit+1;
							for($i=1; $i < $nr; $i++) {
								$y = $i*$limit-$limit;
								echo "| <a href=\"home.php?pos=" . $y . "\">" . $i . "</a> ";
							}
						?>|</td>
						<td style="width:15%;text-align:right;">
						<?php
							if(($num2-$limit) > $pos) {
								echo "<a href=\"home.php?pos=" . $next . "\">" . ADDR_NEXT . "</a> &gt;";
							}
						?></td>
					</tr>
				</table>
			</span>

<?php
	html_end();
?>
